﻿var $edittr = null;
var ServiceId = GetQueryString("Id");
var table;
var draw = 0;
//var dataSourceId = GetQueryString("dataSourceId");
$(function () {
    $("#DataServiceId").val(ServiceId);
    getServiceDetails();
    GetEnumerateTableData();
    /*  GetCodeDataTypeSelectList();
      $("#add-parameter-form").validate({
          submitHandler: function () {
              var jsonobj = {};
              jsonobj.name = $("#add-parameter-Name").val();
              jsonobj.type = $("#add-parameter-DataTypeSelect").val();
              jsonobj.description = $("#add-parameter-Description").val().replace(",", "，");
              jsonobj.defaultValue = $("#add-parameter-DefaultValue").val().replace(",", "，");
              jsonobj.id = "";
              var tr = AssembleParameterData(jsonobj);
              $("#table-details-parameter").append(tr);
              $('#add-parameter-modal').modal('hide');
              ShowGritter("添加成功");
              console.log(JSON.stringify(tr));
          }
      });
      $("#update-parameter-form").validate({
          submitHandler: function () {
              var jsonobj = {};
              jsonobj.name = $("#update-parameter-Name").val();
              jsonobj.type = $("#update-parameter-DataTypeSelect").val();
              jsonobj.description = $("#update-parameter-Description").val().replace(",", "，");
              jsonobj.defaultValue = $("#update-parameter-DefaultValue").val().replace(",", "，");
              jsonobj.id = "";
              if ($edittr != null) {
                  $(AssembleParameterData(jsonobj)).replaceAll($edittr);
              }
  
              $('#update-parameter-modal').modal('hide');
              ShowGritter("编辑成功");
              console.log(JSON.stringify(jsonobj));
          }
      });*/
    $("#AddDataSource").validate({
        submitHandler: function () {
            try {
                //带参数的-弃用
                /* var parstr = "";
                 var parameterlist = $("#table-details-parameter tbody tr");
                 for (var j = 0; j < parameterlist.length; j++) {
                     var tempjson1 = JSON.parse($(parameterlist[j]).attr("data"));
                     parstr += (tempjson1.name + ",");
                     parstr += (tempjson1.type + ",");
                     parstr += (tempjson1.defaultValue + ",");
                     parstr += (tempjson1.description);
                     if (j != (parameterlist.length - 1)) {
                         parstr += ",";
                     }
                 }
                 $("#ParameterSettings").val(parstr);*/
                var url = "DataSourceDetails-RawData.html?Id=" + ServiceId;
                //新增
                $.ajax({
                    url: apiUrl,
                    type: "Post",
                    dataType: "json",
                    data: $("#AddDataSource").serialize(),
                    success: function (data) {
                        if (data.IsSccuess) {
                            ShowGritter("保存成功!", 2, url);
                        } else {
                            if (IsLogined(data.ErrorCode)) {
                                alert("新增失败（" + "错误代码：" + data.ErrorCode + "，错误信息：" + data.Message + ")");
                            }
                        }
                    },
                    beforeSend: function () {
                        ShowGritter("正在提交");
                    }
                });

            } catch (e) {
                console.log(e);
            }
        }
    });
    $("#Chose-Form").submit(function () {
        var $this = $(".chosevalue:checked");
        $("#Value").val($this.val());
        $("#Value-Text").val($this.attr("data"));
        $("#Chose-Div").modal("hide");
        return false;
    });
});
/*
//组合参数数据  编辑时需调用此方法处理返回的数据
//传入的参数类型为对象数组
var AppadParameterData = function (data) {
    //var list = new Array();
    var trlist = "";
    for (var i = 0; i < data.length; i++) {
        var $this = data[i];
        //list.push({ name: $this.Name, id: $this.Id, type: $this.Type, description: $this.Description });
        trlist += AssembleParameterData({ name: $this.Name, id: $this.Id, type: $this.Type, defaultValue: $this.DefaultValue, description: $this.Description });
    }
    $("#table-details-parameter").append(trlist);
}
//传入参数对象返回一行的表格数据
var AssembleParameterData = function (data) {
    var tr = "<tr data='" + JSON.stringify(data) + "'><td>";
    tr += (data.name + "</td>");
    tr += ("<td>" + data.type + "</td>");
    tr += ("<td>" + data.defaultValue + "</td>");//默认值
    tr += ("<td>" + data.description + "</td>");//备注
    tr += ("<td>  <a href='javascript:void(0)' onclick=\"UpateParameterData(this)\"><i class='fa fa-pencil-square-o '></i>编辑</a>&nbsp;<a href='javascript:void(0)' onclick=\"DeleteData(this)\"><i class='fa  fa-times-circle'></i>删除</a></td></tr>");
    return tr;
}
*/
//获取服务的详细信息
var getServiceDetails = function () {
    $.ajax({
        url: apiUrl,
        data: {
            "sys:sno": "ETL",
            "sys:act": "GetDataService",
            "DataServiceId": ServiceId
        },
        dataType: "json",
        success: function (data) {
            if (data.IsSccuess) {
                $("#ServiceName").val(data.Data.Name);
            } else {
                if (IsLogined(data.ErrorCode)) {
                    alert(data.Message);
                }
            }
        },
        beforeSend: function () {
            // $("btn_login").html("登录中...");
        }
    });
}
//传入的参数类型为对象数组
var GetEnumerateTableData = function () {
    if (draw == 0) {
        try {
            table = $('#table-details-value').dataTable({
                "autoWidth": true,
                "oLanguage": dataTableConfig,
                "bLengthChange": false,
                ordering: false,
                bProcessing: true,
                serverSide: true,
                searching: false,
                iDisplayLength: 15,
                sScrollY: '250px',
                "ajax": {
                    url: apiUrl,
                    "dataSrc": function (json) {
                        if (json.IsSccuess) {
                            var newjson = new Array();
                            for (var i = 0; i < json.Data.Items.length; i++) {
                                var $this = json.Data.Items[i];
                                var option = ("<input type='radio' name='chosevalue' class='chosevalue' onclick='$(\"#Chose-Form\").submit();'  value='") + $this.Id + "' data='" + $this.Name + "'/>" + $this.Name;
                                newjson.push({ "option": option });
                            }
                            return newjson;
                        } else {
                            if (IsLogined(json.ErrorCode)) {
                                alert("失败（" + "错误代码：" + json.ErrorCode + "，错误信息：" + json.Message + ")");
                            }
                            return [];
                        }

                    },
                    "data": function (d) {
                        draw = d.draw;
                        //alert(JSON.stringify(d));
                        return $.extend({}, { "sys:sno": "RDS" }, { "sys:act": "EnumerateTable" }, { "draw": d.draw }, { "length": d.length }, { "DataServiceId": ServiceId }, { "Filter": $("#Filter").val() }, { "Page": parseInt(d.start / d.length) });
                    }
                },
                columns: [
                { data: 'option',sWidth:'50%' }
                ],
                fnDrawCallback: function () {
                    table.$('tr').click(function () {
                        $(this).find(".chosevalue")[0].click();
                       
                    });
                    $("#table-details-value_info").parent().removeClass("col-sm-6").addClass("col-sm-4");
                    $("#table-details-value_paginate").parent().removeClass("col-sm-6").addClass("col-sm-8");
                }
            });
        } catch (e) {
            console.log(e);
        }
    } else {
        table.fnDraw();
    }
    //$.ajax({
    //    url: apiUrl,
    //    data: {
    //        "sys:sno": "RDS",
    //        "sys:act": "EnumerateTable",
    //        "DataServiceId": ServiceId
    //    },
    //    dataType: "json",
    //    success: function (data) {
    //        if (data.IsSccuess) {
    //            var trlist = "";
    //            for (var i = 0; i < data.Data.Items.length; i++) {
    //                var $this = data.Data.Items[i];
    //                trlist += ("<tr><td>" + $this.Name + "</td>");
    //                trlist += ("<td><input type='radio' name='chosevalue' class='chosevalue'  value='" + $this.Id + "'/></td></tr>");
    //            }
    //            $("#table-details-value").append(trlist);
    //        } else {
    //            if (IsLogined(data.ErrorCode)) {
    //                alert(data.Message);
    //            }
    //        }
    //    },
    //    beforeSend: function () {
    //        // $("btn_login").html("登录中...");
    //    }
    //});

}
/*
//获取可用代码的select列表
var GetCodeDataTypeSelectList = function () {
    var list = GetCodeDataType();
    var str = "";

    for (var i = 0; i < list.length; i++) {
        str += ("<option value=\"" + list[i] + "\">" + list[i] + "</option>");
    }
    $("#add-parameter-DataTypeSelect").append(str);
    $("#update-parameter-DataTypeSelect").append(str);
}
//删除当前行数据
var DeleteData = function (obj) {
    if (confirm("是否删除？")) {
        $(obj).parent().parent().remove();
    }
}
//编辑传入参数按钮触发的操作
var UpateParameterData = function (obj) {
    var tr = $(obj).parent().parent();
    $edittr = tr;
    var json = tr.attr("data");
    var jsonobj = JSON.parse(json);
    $("#update-parameter-Name").val(jsonobj.name);
    $("#update-parameter-DataTypeSelect").val(jsonobj.type);
    $("#update-parameter-Description").val(jsonobj.description);
    $("#update-parameter-DefaultValue").val(jsonobj.defaultValue);
    $('#update-parameter-modal').modal({
        show: true
    });
}
//新增传入参数按钮触发的操作
var AddParameter = function () {
    document.getElementById('add-parameter-form').reset();
    $('#add-parameter-modal').modal({
        show: true
    });
}*/